/**
 * @param {number[]} gain
 * @return {number}
 */
const largestAltitude1 = function (gain) {
    let last = 0;
    let n = gain.length;
    for (let i=0; i<n; ++i) {
        if (i === 0) {
            last = gain[0];
            gain[i] = 0;
        } else {
            let temp = gain[i];
            gain[i] = gain[i-1] + last;
            last = temp;
        }
    }
    gain.push(gain[n-1] + last);
    console.log(gain);
    // return Math.max(gain); // 报错的，Math.max()不能用于数组！！
    return getArrayMax(gain);
};

/**
 * @param {number[]} arr
 * @return {number}
 */
const getArrayMax = function (arr) {
    let max = arr[0];
    for (let i = 1; i <= arr.length; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
};


/**
 * @param {number[]} gain
 * @return {number}
 * 前缀和
 */
const largestAltitude = function (gain) {
    let ans = 0, sum = 0;
    for (const x of gain) {
        sum += x;
        ans = Math.max(ans, sum);
    }
    return ans;
};

// 外部文件夹调用
module.exports = largestAltitude;